﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerfilEmpreendedor.Dao.Interfaces;
using NHibernate;
using PerfilEmpreendedor.Dao.Factory;

namespace PerfilEmpreendedor.Dao.Implements
{
    public abstract class HibernateDao<T> : IHibernateDao<T>
    {
        protected readonly ISession session = HttpModule.GetSession;
        public void Save(T obj)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                try
                {
                    session.Save(obj);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    session.Clear();
                    session.Flush();
                }
            }
        }

        public void SaveOrUpdate(T obj)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                try
                {
                    session.SaveOrUpdate(obj);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    session.Clear();
                    session.Flush();
                }
            }
        }

        public void Update(T obj)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                try
                {
                    session.Update(obj);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    session.Clear();
                    session.Flush();
                }
            }
        }

        public void Delete(T obj)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                try
                {
                    session.Delete(obj);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    session.Clear();
                    session.Flush();
                }
            }
        }

        public T Find(object id)
        {
            return session.Load<T>(id);
        }
        public IList<T> List(String fieldOrder)
        {
            IList<T> lst = session.CreateCriteria(typeof(T))
                .AddOrder(new NHibernate.Criterion.Order(fieldOrder, true))
                .List<T>();
            
            return lst;
        }
    }
}
